package com.nitty.cash.loop.ui.dialog

import android.content.Context
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import com.nitty.cash.loop.R
import com.nitty.cash.loop.common.CommonDialog
import com.nitty.cash.loop.databinding.DialogCommentScoreBinding
import com.nitty.cash.loop.databinding.DialogCommentScoreStarItemBinding
import com.nitty.cash.loop.event.NittyEventPointHelper
import com.nitty.cash.loop.event.NittyEventPointType
import com.nitty.cash.loop.utils.helper.NittyCommentDialogHelper
import org.json.JSONObject

/**
 * NittyCash
 * @Author wwdeng
 * @CreateTime: 2024/11/13 16:26
 * @Annotation：评分 Normal
 */
class NittyCommentScoreDialog: CommonDialog {
    private lateinit var binding: DialogCommentScoreBinding
    private var mCallback: NittyCommentDialogHelper.IStarDialogCallback? = null

    private val normalIconList = arrayListOf(R.mipmap.nitty_comment_score1_normal,R.mipmap.nitty_comment_score2_normal,R.mipmap.nitty_comment_score3_normal,R.mipmap.nitty_comment_score4_normal,R.mipmap.nitty_comment_score5_normal)
    private val selectedIconList = arrayListOf(R.mipmap.nitty_comment_score1_selected,R.mipmap.nitty_comment_score2_selected,R.mipmap.nitty_comment_score3_selected,R.mipmap.nitty_comment_score4_selected,R.mipmap.nitty_comment_score5_selected)
    private var scoreList: ArrayList<JSONObject>? = null
    private var selectStar = -1

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: NittyCommentDialogHelper.IStarDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: NittyCommentDialogHelper.IStarDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogCommentScoreBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        binding.btnNotNow.paint.flags = Paint.UNDERLINE_TEXT_FLAG//下划线
        binding.btnNotNow.paint.isAntiAlias = true//抗锯齿
    }

    override fun initListener() {
        setOnClickListener(binding.btnNotNow)
        setOnClickListener(binding.btnRate)
//        binding.ratingBar.setIsIndicator(false)
//        binding.ratingBar.setOnRatingChangeListener { ratingBar, rating, fromUser ->
//            NittyEventPointHelper.addEventPointRequestAction(NittyEventPointType.CLICK.NITTY_CLICK_USER_RATE_US_STAR,rating.toInt().toString())
//        }
    }

    override fun loadData() {
        if (scoreList == null){
            scoreList = arrayListOf()
        }
        for (i in 0..<normalIconList.size){
            val item = JSONObject()
            item.put("iconNormal",normalIconList[i])
            item.put("iconSelected",selectedIconList[i])
            scoreList?.add(item)
        }
        showNittyCommentScoreViews()
    }

    override fun onViewClick(v: View?) {
        when(v!!.id){
            R.id.btnNotNow -> {//关闭、取消
                NittyEventPointHelper.addEventPointRequestAction(NittyEventPointType.CLICK.NITTY_CLICK_USER_RATE_US_NOT_NOW,"")
                dismiss()
            }
            else -> {//确定

                if (selectStar+1 <= 0){
                    return
                }
                if(mCallback != null){
                    mCallback!!.completeScoringCount(selectStar+1)
                }
                dismiss()
            }
        }
    }

    fun setScoreTextTips(tips: String){
        binding.scoreTips.text = tips
    }

    /**
     * 设置评分
     */
    private fun showNittyCommentScoreViews(){
        binding.starCountLayout.removeAllViews()
        for (i in 0..<scoreList!!.size){
            val item = scoreList!![i]
            val itemBinding = DialogCommentScoreStarItemBinding.inflate(layoutInflater)
            if (i <= selectStar){
                itemBinding.imgCommentStar.setImageResource(item.optInt("iconSelected"))
            } else {
                itemBinding.imgCommentStar.setImageResource(item.optInt("iconNormal"))
            }
            itemBinding.imgCommentStar.setOnClickListener {
                selectStar = i
                NittyEventPointHelper.addEventPointRequestAction(NittyEventPointType.CLICK.NITTY_CLICK_USER_RATE_US_STAR,(selectStar+1).toString())
                showNittyCommentScoreViews()
            }
            binding.starCountLayout.addView(itemBinding.root)
        }
    }

    override fun onStart() {
        super.onStart()
        NittyEventPointHelper.addEventPointRequestAction(NittyEventPointType.DIALOG.NITTY_DIALOG_USER_RATE_SHOW,"Normal Dialog")
    }

    override fun dismiss() {
        super.dismiss()
        NittyEventPointHelper.addEventPointRequestAction(NittyEventPointType.DIALOG.NITTY_DIALOG_USER_RATE_DISMISS,"Normal Dialog")
    }
}